/**
 * 系统管理--活动管理的单例对象
 */
var activity = {
    id: "activityTable", //表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    condition: {
        startTime: "",
        endTime: ""
    }
};

/**
 * 初始化表格的列
 */
activity.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {
            title: '序号', field: 'number', width: 1, align: 'center', valign: 'middle', formatter: function (value, row, index) {
                return index + 1;
            }
        },
        {title: 'activityId', field: 'activityId', visible: false, align: 'center', valign: 'middle'},
        {title: '活动名称', field: 'activityName', align: 'center', valign: 'middle', sortable: true},
        {title: '活动内容', field: 'activityNote', align: 'center', valign: 'middle', sortable: true},
        {title: '折扣力度', field: 'activityDiscount', align: 'center', valign: 'middle', sortable: true, formatter: function (value, row, index) {
                return  '租车' + row.activityDiscount + '折'
            }},
        {title: '开始时间', field: 'startTime', align: 'center', valign: 'middle', sortable: true},
        {title: '结束时间', field: 'endTime', align: 'center', valign: 'middle', sortable: true},
        {title: '是否过期', field: 'isTimeout', align: 'center', valign: 'middle', sortable: true, formatter: function(value, row, index){
            if (row.isTimeout === 1) return '<button type="button" class="btn btn-xs btn-warning" readonly >已过期</button>'
            return '<button type="button" class="btn btn-xs btn-success" onclick="activity.toGetById()">点击领取</button>'
            }
        }
    ];
};

/**
 * 检查是否选中
 */
activity.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length === 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        activity.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加
 */
activity.openAddActivity = function () {
    this.layerIndex = layer.open({
        type: 2,
        title: '添加活动',
        area: ['500px', '580px'],
        fix: false,
        maxmin: true,
        content: Feng.ctxPath + '/activity/activity_add'
    });
};

/**
 * 点击修改按钮时
 */
activity.openEditActivity = function (activityId) {
    if(!activityId){
        if (!this.check()) {
            return;
        }
    }
    var selectId = activityId;
    if (!activityId) {
        selectId = this.seItem.activityId;
    }
    this.layerIndex = layer.open({
        type: 2,
        title: '编辑活动',
        area: ['500px', '580px'],
        fix: false,
        maxmin: true,
        content: Feng.ctxPath + '/activity/activity_edit?activityId=' + selectId
    });
};

/**
 * 点击删除按钮
 */
activity.deleteActivity = function () {
    if (this.check()) {
        var operation = function () {
            var activityId = activity.seItem.activityId;
            var ajax = new $ax(Feng.ctxPath + "/activity/delete", function () {
                Feng.success("删除成功!");
                activity.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("activityId", activityId);
            ajax.start();
        };
        Feng.confirm("是否删除该活动?", operation);
    }
};

activity.toGetById = function(){
    if (this.check()) {
        var operation = function () {
            var activityId = activity.seItem.activityId;
            var ajax = new $ax(Feng.ctxPath + "/activity/toGetById", function () {
                Feng.success("领取成功!");
                activity.table.refresh();
            }, function (data) {
                Feng.error("领取失败!" + data.responseJSON.message + "!");
            });
            ajax.set("activityId", activityId);
            ajax.start();
        };
        Feng.confirm("是否领取该活动?", operation);
    }
};


/**
 * 点击查询按钮
 */
activity.search = function () {
    activity.condition.startTime = Feng.getStartTime();
    activity.condition.endTime = Feng.getEndTime();
    var queryData = {};
    queryData['startTime'] = activity.condition.startTime;
    queryData['endTime'] = activity.condition.endTime;
    queryData['timeLimit'] = $("#timeLimit").val();
    activity.table.refresh({query: queryData});
};



$(function () {
    activity.app = new Vue({
        el: '#activityManagerPage',
        data: activity.condition
    });
    var defaultColunms = activity.initColumn();
    var table = new BSTable("activityTable", "/activity/list", defaultColunms);
    table.setPaginationType("client");
    activity.table = table.init();
    laydate.render({
        elem: '#timeLimit',
        range: true,
        theme: '#009efb',
        max: Feng.currentDate()
    });
    Feng.initStartTime(activity.condition.startTime);
    Feng.initEndTime(activity.condition.endTime);
});
